﻿#region Usings

using System;
using System.Collections.Generic;
using System.Web;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Interfaces.Activity;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.Activity;
using Rodunu.Movies.Logic.User;
using Rodunu.Utilities.Email;
using Rodunu.Movies.Logic.Movie;

#endregion

namespace Rodunu.Movies.Web.Controllers
{
    public class FriendsController : UserBaseController
    {
        #region Private Members

        private readonly IFriends _friendsLogic;
        private readonly IUser _userLogic;

        #endregion

        #region Constructors

        public FriendsController()
        {
            _friendsLogic = new FriendsLogic();
            _userLogic = new UserLogic();
        }

        #endregion

        public void FriendsList()
        {
            SetUser();
            SetPage(PageTypes.Freinds);
            SetLayout(true);

            PropertyBag["friends"] = _friendsLogic.GetFriendsForUser(User.Id);
            //IList<UserInfo> friendsU = new List<UserInfo>();
            //foreach (var friend in friends)
            //{
            //    RenderText(friend.Friend.UserName);
            //    friendsU.Add(friend.Friend);
            //}

            ////PropertyBag["friends"] = friendsU;
        }

        public void AddFriend(string userName)
        {
            SetUser();
            UserInfo friend = _userLogic.GetByName(userName);
            if (friend.Id != User.Id)
            {
                UserInfo currentUser = _userLogic.Find(User.Id);

                if (friend.Id > 0)
                {
                    Friends newFriend = new Friends();
                    newFriend.DateAdded = DateTime.Now;
                    newFriend.DateAddedBy = currentUser;
                    newFriend.DeleteFriend = false;
                    newFriend.Friend = friend;
                    newFriend.LastModified = DateTime.Now;
                    newFriend.LastModifiedBy = currentUser;
                    newFriend.UserInfo = currentUser;
                    newFriend.Pending = true;

                    _friendsLogic.Create(newFriend);

                    IUserActivity userActivityLogic = new UserActivityLogic();
                    userActivityLogic.Create(new UserActivity(_userLogic.Find(User.Id), "Adding Friend", DateTime.Now, (int)UserActivityTypes.AddFriend, newFriend.Id));

                    var postOffice = new PostOffice();
                    postOffice.SendFriendRequest(friend.EmailAddress, friend.UserName, User.UserName);

                    Redirect("Friends", "FriendsList");
                }
                else
                    throw new Exception("Invalid Friend Id");
            }
            else
                throw  new Exception("Cannot add yourself as a friend");
        }
    
        public void AcceptFriend(long friendId)
        {
            SetUser();
            _friendsLogic.SetFriend(friendId, false);
            Friends friends = _friendsLogic.Find(friendId);

            var postOffice = new PostOffice();
            postOffice.ConfirmFriendRequest(friends.Friend.EmailAddress, User.UserName, User.UserName);

            IUserActivity userActivity = new UserActivityLogic();
            userActivity.Create(new UserActivity(_userLogic.Find(User.Id), "Adding Friend", DateTime.Now, (int)UserActivityTypes.AddFriend, friends.Id));

            RenderText(friendId.ToString());
        }

        public void DeclineFriend(long friendId)
        {
            SetUser();
            _friendsLogic.SetFriend(friendId, false);
            RenderText(friendId.ToString());
        }

        public void RequestMovie(long userMovieId, long friendId)
        {
            SetUser();
            IFriendMovieRequest movieRequestLogic = new FriendMovieRequestLogic();
            FriendMovieRequest movieRequest = movieRequestLogic.CheckIfMovieAvailable(userMovieId);
            if (movieRequest != null && movieRequest.Id > 0)
            {
                RenderText("0:Movie is not available");
                return;
            }

            IUserMovies userMovieLogic = new UserMoviesLogic();
            var newRequest = new FriendMovieRequest();
            newRequest.RequestDate = DateTime.Now;
            newRequest.UserInfo = _userLogic.Find(User.Id);
            newRequest.UserMovies = userMovieLogic.Find(userMovieId);
            movieRequestLogic.Create(newRequest);

            var postOffice = new PostOffice();
            postOffice.MovieBorrowRequest(newRequest.UserMovies.UserInfo.EmailAddress, newRequest.UserInfo.Name,
                newRequest.UserMovies.Name);

            IUserMoviesStatus userMoviesStatusLogic = new UserMoviesStatusLogic();
            userMoviesStatusLogic.SetUserMovieStatus(userMovieId, 3);

            RenderText(newRequest.UserMovies.Id + ":Request Sent");
        }

        public void CheckIfMovieAvailable(long userMovieId)
        {
            SetUser();
            IFriendMovieRequest movieRequestLogic = new FriendMovieRequestLogic();
            FriendMovieRequest movieRequest = movieRequestLogic.GetByMovieId(userMovieId);
            if (movieRequest != null && movieRequest.Id > 0)
            {
                if (movieRequest.ApproveDate == null)
                    CheckIfCurrentUser(movieRequest, ":Movie Requested to Borrow");
                else
                {
                    if (movieRequest.ReturnDate == null)
                    {
                        if (movieRequest.Approved == null || movieRequest.Approved == true)
                            CheckIfCurrentUser(movieRequest, ":Movie Approved to Borrow");
                        else
                            RenderText("0");
                    }
                    else
                        RenderText("0");
                }
            }
            else
                RenderText("0");
        }

        private void CheckIfCurrentUser(FriendMovieRequest movieRequest, string message)
        {
            if (movieRequest.UserInfo.Id == User.Id)
                RenderText(movieRequest.UserMovies.Movie.Id + message);
            else
                RenderText(movieRequest.UserMovies.Movie.Id + ":Movie Unavailable");
        }

        public void AllowRequest(long requestId, bool allow, string reason)
        {
            SetUser();
            IFriendMovieRequest movieRequestLogic = new FriendMovieRequestLogic();
            var movieRequest = movieRequestLogic.Find(requestId);

            movieRequest.Approved = allow;
            movieRequest.ApproveDate = DateTime.Now;
            movieRequestLogic.Update(movieRequest);

            var postOffice = new PostOffice();
            if (allow)
            {
                IUserMoviesStatus userMoviesStatusLogic = new UserMoviesStatusLogic();
                userMoviesStatusLogic.SetUserMovieStatus(movieRequest.UserMovies.Id, 2);
                postOffice.MovieRequestAccepted(movieRequest.UserInfo.EmailAddress,
                                                movieRequest.UserMovies.UserInfo.Name,
                                                movieRequest.UserMovies.Name, HttpUtility.UrlDecode(reason));
            }
            else
                postOffice.MovieRequestDenied(movieRequest.UserInfo.EmailAddress, movieRequest.UserMovies.UserInfo.Name,
                    movieRequest.UserMovies.Name, HttpUtility.UrlDecode(reason));

            RenderText(requestId.ToString());
        }

        public void ReturnMovie(long requestId)
        {
            SetUser();
            IFriendMovieRequest movieRequestLogic = new FriendMovieRequestLogic();
            var movieRequest = movieRequestLogic.Find(requestId);
            movieRequest.ReturnDate = DateTime.Now;
            movieRequestLogic.Update(movieRequest);

            IUserMoviesStatus userMoviesStatusLogic = new UserMoviesStatusLogic();
            userMoviesStatusLogic.SetUserMovieStatus(movieRequest.UserMovies.Id, 1);

            RenderText(requestId.ToString());
        }
    }
}
